﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Fynd.Hermes.Web.Objects;

namespace Fynd.Hermes.Web.Pages
{
    public abstract class RoleBasedPage : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            UserInfomation theUser = Context.Items[Global.HttpContextKeyUser] as UserInfomation;
            bool denying = false;
            if (null == theUser)
            {
                denying = true;
            }
            else
            {
                IEnumerable<UserRole> theAccreditedRoles = AccreditedRoles;
                if (null == theAccreditedRoles)
                {
                    denying = true;
                }
                else if (false == theAccreditedRoles.Contains<UserRole>(theUser.Role))
                {
                    denying = true;
                }
            }

            if (denying == true)
            {
                Response.Redirect("~/Login.aspx?msg=" + Server.UrlEncode("没有权限访问这个页面，换个帐号吧"));
            }
        }

        protected abstract IEnumerable<UserRole> AccreditedRoles
        {
            get;
        }
    }
}